#!c:\python27\python.exe -u
# -*- encoding: gb2312 -*-
import os
import sys
import urllib

import re
import shutil
import time
import threading
import Queue

def dld():
    while True:
        mp3, fn = q.get()
        print "From Queue I get task: ", mp3, fn
        try:
            d = urllib.urlopen(mp3)
            info = d.info()
            print(info)
            if info['Content-Type'] == "audio/mpeg" and int(info['Content-Length']) <  10000000 and int(info['Content-Length']) >  100000:
                f = open(fn, "wb")
                print ("Save as: ", fn)
                f.write(d.read())
                f.close()
            else:
                print ("The source is not good enough.")
        except Exception as e:
            print (e)     
        q.task_done()        
              
# thread pool 
q = Queue.Queue(5)
for i in range(8):
    t = threading.Thread(target=dld)
    t.daemon = True
    t.start()

page = r'http://freekidsmusic.com/traditional-childrens-songs/'
request = urllib.urlopen(page)
html = request.read()
request.close()
raw = re.findall(r'<li><a href="http://freekidsmusic.com/traditional-childrens-songs/[^/]*/">.*</li>', str(html), re.MULTILINE)

print len(raw)
for r in raw:
    #extract actual url and mp3 name
    m = re.compile(r'(http[^"]*).*$')
    href = m.search(r).groups()[0]
    print href
    request = urllib.urlopen(href)
    html = request.read()
    request.close()
    mp3_raw = re.findall(r'<a href="http://freekidsmusic.com/[^"]*.mp3">.*</a>', str(html), re.MULTILINE)
    #print mp3_raw
    m = re.compile(r'(http[^"]*).*$')
    href=m.search(mp3_raw[0]).groups()
    print href[0]
    m = re.compile(r'([^/]*mp3)')
    name=m.search(href[0]).groups()
    print name[0]
    q.put([href[0], name[0]], True)
    print "added."
